package day7;

import java.util.Stack;

//合法括号判断
public class oj1 {
        public boolean chkParenthesis(String A, int n) {
            // write code here

            if(n % 2 == 1) {
                return false;
            }

            Stack<Character> stack = new Stack<>();
            for(char c : A.toCharArray()) {
                if(c == '(') {
                    stack.push(c);
                } else if(c == ')') {
                    if(stack.isEmpty()) {
                        return false;
                    } else if(stack.peek() == ')') {
                        stack.pop();
                    }
                } else {
                    return false;
                }

            }
            return !stack.isEmpty();
        }
    }
